package com.bj.zspace.cat.controller;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @Author:liuwenqing
 * @Date:2024/12/13 15:40
 * @Description:
 **/
@RestController
public class MatrixTest {

    @GetMapping(value = "/matrix/url")
    public String matrix() {

        Transaction t = Cat.newTransaction("URL", "/matrix/url");
        try {
            Transaction t1 = Cat.newTransaction("Call", "app1");
            TimeUnit.SECONDS.sleep(1);
            t1.setStatus(Message.SUCCESS);
            t1.complete();
            Transaction t2 = Cat.newTransaction("SQL", "database");
            TimeUnit.SECONDS.sleep(1);
            t2.setStatus(Message.SUCCESS);
            t2.complete();
            Transaction t3 = Cat.newTransaction("Cache.user", "user");
            TimeUnit.SECONDS.sleep(1);
            t3.setStatus(Message.SUCCESS);
            t3.complete();
            t.setStatus(Message.SUCCESS);
        } catch (Exception e) {
            t.setStatus(e);
        } finally {
            t.complete();
        }
        return "ok";
    }

    @GetMapping(value = "/matrix/Service")
    public String Service() {

        Transaction t = Cat.newTransaction("Service", "s1");
        try {
            Transaction t1 = Cat.newTransaction("Call", "app1");
            TimeUnit.SECONDS.sleep(1);
            t1.setStatus(Message.SUCCESS);
            t1.complete();
            Transaction t2 = Cat.newTransaction("SQL", "database");
            TimeUnit.SECONDS.sleep(1);
            t2.setStatus(Message.SUCCESS);
            t2.complete();
            Transaction t3 = Cat.newTransaction("Cache.user", "user");
            TimeUnit.SECONDS.sleep(1);
            t3.setStatus(Message.SUCCESS);
            t3.complete();
            t.setStatus(Message.SUCCESS);
        } catch (Exception e) {
            t.setStatus(e);
        } finally {
            t.complete();
        }
        return "ok";
    }

    @GetMapping(value = "/matrix/pigeonService")
    public String pigeonService() {

        Transaction t = Cat.newTransaction("PigeonService", "s2");
        try {
            Transaction t1 = Cat.newTransaction("Call", "app1");
            TimeUnit.SECONDS.sleep(1);
            t1.setStatus(Message.SUCCESS);
            t1.complete();
            Transaction t2 = Cat.newTransaction("SQL", "database");
            TimeUnit.SECONDS.sleep(1);
            t2.setStatus(Message.SUCCESS);
            t2.complete();
            Transaction t3 = Cat.newTransaction("Cache.user", "user");
            TimeUnit.SECONDS.sleep(1);
            t3.setStatus(Message.SUCCESS);
            t3.complete();
            t.setStatus(Message.SUCCESS);
        } catch (Exception e) {
            t.setStatus(e);
        } finally {
            t.complete();
        }
        return "ok";
    }

}
